package net.awpspace.matheasy;

import java.util.Hashtable;

import javax.microedition.io.ConnectionNotFoundException;
import javax.microedition.lcdui.Alert;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.midlet.MIDlet;

import vAdEngine.VservInterface;
import vAdEngine.VservManager;

public class MathEasy extends MIDlet implements VservInterface {

	private boolean midletPaused = false;

	String zoneID = "f68109b0";
	// For Mid Ad
	private Displayable vservDisplayable;

	/**
	 * The MyMidlet constructor.
	 */
	public Display display;

	public MathEasy() {
		display = Display.getDisplay(this);
	}

	/**
	 * Initilizes the application. It is called only once when the MIDlet is
	 * started. The method is called before the <code>startMIDlet</code> method.
	 */
	private void initialize() {
	}

	/**
	 * Performs an action assigned to the Mobile Device - MIDlet Started point.
	 */
	public void startMIDlet() {
		// write pre-action user code here
		switchDisplayable(null, new MenuScreen(this));
		// write post-action user code here
	}

	/**
	 * Performs an action assigned to the Mobile Device - MIDlet Resumed point.
	 */
	public void resumeMIDlet() {
	}

	/**
	 * Switches a current displayable in a display. The <code>display</code>
	 * instance is taken from <code>getDisplay</code> method. This method is
	 * used by all actions in the design for switching displayable.
	 * 
	 * @param alert
	 *            the Alert which is temporarily set to the display; if
	 *            <code>null</code>, then <code>nextDisplayable</code> is set
	 *            immediately
	 * @param nextDisplayable
	 *            the Displayable to be set
	 */
	public void switchDisplayable(Alert alert, Displayable nextDisplayable) {
		Display display = getDisplay();
		if (alert == null) {
			display.setCurrent(nextDisplayable);
		} else {
			display.setCurrent(alert, nextDisplayable);
		}
	}

	/**
	 * Called by a system to indicated that a command has been invoked on a
	 * particular displayable.
	 * 
	 * @param command
	 *            the Command that was invoked
	 * @param displayable
	 *            the Displayable where the command was invoked
	 */
	public void showMidAd() {
		Hashtable vservConfigTableAd = new Hashtable();
		vservConfigTableAd.put("zoneId", zoneID);
		vservConfigTableAd.put("viewMandatory", "true");
		vservConfigTableAd.put("showAt", "mid");
		new VservManager(this, vservConfigTableAd);
	}

	/**
	 * Returns a display instance.
	 * 
	 * @return the display instance.
	 */
	public Display getDisplay() {
		return Display.getDisplay(this);
	}

	/**
	 * Exits MIDlet.
	 */
	public void exitMIDlet() {
		switchDisplayable(null, null);
//		destroyApp(true);
//		notifyDestroyed();
		Hashtable vservConfigTableAd = new Hashtable();
		vservConfigTableAd.put("zoneId", zoneID);
		vservConfigTableAd.put("viewMandatory", "true");
		vservConfigTableAd.put("showAt", "end");
		new VservManager(this, vservConfigTableAd);
	}

	/**
	 * Called when MIDlet is started. Checks whether the MIDlet have been
	 * already started and initialize/starts or resumes the MIDlet.
	 */
	public void startApp() {
		Hashtable vservConfigTableAd = new Hashtable();
		vservConfigTableAd.put("zoneId", zoneID);
		vservConfigTableAd.put("viewMandatory", "true");
		vservConfigTableAd.put("showAt", "start");
		new VservManager(this, vservConfigTableAd);
	}

	/**
	 * Called when MIDlet is paused.
	 */
	public void pauseApp() {
		midletPaused = true;
	}

	/**
	 * Called to signal the MIDlet to terminate.
	 * 
	 * @param unconditional
	 *            if true, then the MIDlet has to be unconditionally terminated
	 *            and all resources has to be released.
	 */
	public void destroyApp(boolean unconditional) {
	}

	public void resumeMainApp() {
		Display.getDisplay(this).setCurrent(vservDisplayable);
	}

	public void startMainApp() {
		if (midletPaused) {
			resumeMIDlet();
		} else {
			initialize();
			startMIDlet();
		}
		midletPaused = false;
	}

	public void constructorMainApp() {

	}

	public void more() {
		try {
			platformRequest("http://store.ovi.com/publisher/withlovei/?clickSource=search");
		} catch (ConnectionNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
